#!/bin/sh

PAGER=${PAGER:-less}
wait=less

while getopts 'lc' opt; do
    case $opt in
	l  ) wait=less ;;
	c  ) wait=cat ;;
	\? ) echo 'usage: nsfm.hold [-l] [-c] command [args...]'
	     exit 1
    esac
done
shift $(($OPTIND - 1))

execute ()
{
   "$@" 2>&1
}

if [ "$wait" = "less" ]
then
   execute "$@" | $PAGER
   exit $?
fi

if ! [ "$wait" = "cat" ]
then
   echo "error?" >&2
   exit 1
fi

tmpfile=`mktemp "/tmp/nsfm.hold.$USER.XXXXXX"`
trap "rm -f $tmpfile" EXIT HUP TERM QUIT INT PIPE KILL ABRT

execute "$@" 2>&1 | tee $tmpfile &&
   $PAGER $tmpfile

